/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commands;

import entityclasses.Session;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import entityclasses.Group;

/**
 *
 * @author umutcan
 */
public class RemoveGroupCommand implements GroupCommand {
    
    public void execute (JTree treeGruplar)
    {
        TreePath tp = treeGruplar.getSelectionPath();
        DefaultTreeModel tm = (DefaultTreeModel)treeGruplar.getModel();
        
        DefaultMutableTreeNode removedNodeParent = (DefaultMutableTreeNode) ((DefaultMutableTreeNode)tp.getLastPathComponent()).getParent();
        DefaultMutableTreeNode removedNode = (DefaultMutableTreeNode)tp.getLastPathComponent();
        if (removedNodeParent.isRoot())
            Session.currentUser.getProfile().removeGroup((Group)removedNode.getUserObject());
        else
           ((Group)removedNodeParent.getUserObject()).getSubgroups().remove((Group)removedNode.getUserObject());
        
        tm.removeNodeFromParent((DefaultMutableTreeNode) tp.getLastPathComponent());
    }
}
